//
//  CubeAdTextView.m
//  easymarketing
//
//  Created by chenwei on 15/5/20.
//  Copyright (c) 2015年 cubead. All rights reserved.
//

#import "CubeAdTextView.h"

@interface CubeAdTextView ()
// 提示信息label
@property (nonatomic, weak) UILabel *placehoderLabel;

@end

@implementation CubeAdTextView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        // 添加提示信息文字的label
        UILabel *placehoderLabel = [[UILabel alloc] init];
        placehoderLabel.numberOfLines = 0;
        placehoderLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:placehoderLabel];
        self.placehoderLabel = placehoderLabel;
       
        self.font = [UIFont systemFontOfSize:13];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}
#pragma mark - textdidchange
- (void)textDidChange {
    
    self.placehoderLabel.hidden = (self.text.length != 0);
}

- (void)setText:(NSString *)text {
    
    [super setText:text];
    
    [self textDidChange];
}

- (NSString *)getText {
    return self.text;
}
- (void)setPlacehoder:(NSString *)placehoder {
    
    _placehoder = [placehoder copy];
    
    //设置文字
    self.placehoderLabel.text = placehoder;
    
    // 重新计算字空间
    [self setNeedsLayout];
    
}

- (void)setPlacehoderColor:(UIColor *)placehoderColor {
    
    _placehoderColor = placehoderColor;
    // 设置颜色
    self.placehoderLabel.textColor = placehoderColor;
    
}

- (void)setFont:(UIFont *)font {
    
    [super setFont:font];
    
    self.placehoderLabel.font = font;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    self.placehoderLabel.y = 8;
    self.placehoderLabel.x = 5;
    self.placehoderLabel.width = self.width - 2 * self.placehoderLabel.x;
    // 根据文字计算label的高度
    CGSize maxSize = CGSizeMake(self.placehoderLabel.width, MAXFLOAT);
    CGSize placehoderSize = [self.placehoder boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size;
    self.placehoderLabel.height = placehoderSize.height;
}

#pragma mark -移除通知
- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
